﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ELab.Student
{
    public class HBlockInput
    {
        globalKeyboardHook gkh = new globalKeyboardHook();
        globalMouseHook gmh = new globalMouseHook();

        public BlockInputState BlockInputState { get; private set; }
        public HBlockInput()
        {
            gkh = new globalKeyboardHook();
            gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
            gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
            // gkh.MouseDown += new MouseEventHandler(MouseClick);
            gmh.MouseAction += new EventHandler(MouseClick);
            this.BlockInputState = Student.BlockInputState.Normal;

        }
        ~HBlockInput()
        {
            StopBlock();
        }
        public void StartBlock()
        {
            if (this.BlockInputState == Student.BlockInputState.Blocked) return;
            gkh.hook();
            gmh.Hook();
            this.BlockInputState = Student.BlockInputState.Blocked;
        }
        public void StopBlock()
        {
            if (this.BlockInputState == Student.BlockInputState.Normal) return;
            gkh.unhook();
            gmh.UnHook();
            this.BlockInputState = Student.BlockInputState.Normal;
        }
        void gkh_KeyUp(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }
        public void MouseClick(object sender, EventArgs e)
        {
            //labelMousePosition.Text = String.Format("x={0}  y={1}", e.X, e.Y);
            //if (e.Clicks > 0) LogWrite("MouseButton     - " + e.Button.ToString());
            //MessageBox.Show("Mouse click");
            
        }
        void gkh_KeyDown(object sender, KeyEventArgs e)
        {
            
            // MessageBox.Show("Key enter:" + e.KeyCode.ToString());

            if (e.KeyCode == Keys.F12)
            {

                //BlockInputState = Student.BlockInputState.NeedToBlock;
                BlockInputState = Student.BlockInputState.NeedToBlock; 
                // Bo chan phim de con nhap mat khau
                //bool bContinueBlock = true;
                //frmPassword askPass = new frmPassword();
                //if (askPass.ShowDialog() == DialogResult.OK)
                //{
                //    if (askPass.strRetPassword == "hai")
                //        bContinueBlock = false;
                //}
                //if (!bContinueBlock)
                //     StartBlock();//BlockInputState = Student.BlockInputState.Normal;

            }
        }
    }

    public enum BlockInputState
    {
        Normal,
        Blocked,
        NeedToBlock
    }
}
